//
//  FeaturesViewController.m
//  iTrack
//
//  Created by Sergei Sevriugin on 6/13/10.
//  Copyright 2010 Bellwood Systems. All rights reserved.
//

#import "FeaturesViewController.h"
#import "XMLNodeViewController.h"


@implementation FeaturesViewController

@synthesize gmaps;
@synthesize currentMapId;
@synthesize mapProperties;
@synthesize featureProperties;
@synthesize child_indexPath;
@synthesize updated;
@synthesize root_controller;
@synthesize needToSave;
@synthesize saveCurrentFeature;
@synthesize saveCurrentMap;

#pragma mark -
#pragma mark View Data management

- (void) loadFeatureProperties:(GDataEntryMapFeature*)feature {

	// set feature properties

	if (featureProperties) {
		for (int i = 0; i < [featureProperties count]; i++) {
			[[featureProperties objectAtIndex:i] release];
		}
		[featureProperties release];
	}

	if (feature == nil) {
		return;
	}
	
	featureProperties = [[NSMutableArray alloc] initWithCapacity:40];

	NSString*				propertyTitle		= nil;
	NSString*				propertySubtitle	= nil;
	NSInteger				propertyKind		= iTrackPropetyKingInvalid;
	
	iTrackPropety*			property			= nil;
	
	if ([feature customProperties]) {
		propertyTitle		= [NSString stringWithString:@"customProperties [count]"];
		propertySubtitle	= [NSString stringWithFormat:@"%D",[[feature customProperties] count]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
		
		GDataCustomProperty* customProperty		= [[feature customProperties] objectAtIndex:0];
		if (customProperty) {
			if ([customProperty name]) {
				propertyTitle		= [NSString stringWithString:@"customProperty [name]"];
				propertySubtitle	= [NSString stringWithFormat:@"%@",[customProperty name]];
				propertyKind		= iTrackPropetyKingText;
				property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
			
				[featureProperties addObject:property];
			}
			if ([customProperty type]) {
				propertyTitle		= [NSString stringWithString:@"customProperty [type]"];
				propertySubtitle	= [NSString stringWithFormat:@"%@",[customProperty type]];
				propertyKind		= iTrackPropetyKingText;
				property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
			
				[featureProperties addObject:property];
			}
			if ([customProperty unit]) {
				propertyTitle		= [NSString stringWithString:@"customProperty [unit]"];
				propertySubtitle	= [NSString stringWithFormat:@"%@",[customProperty unit]];
				propertyKind		= iTrackPropetyKingText;
				property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
			
				[featureProperties addObject:property];
			}
			if ([customProperty value]) {
				propertyTitle		= [NSString stringWithString:@"customProperty [value]"];
				propertySubtitle	= [NSString stringWithFormat:@"%@",[customProperty value]];
				propertyKind		= iTrackPropetyKingText;
				property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
			
				[featureProperties addObject:property];
			}
		}
	}

	if ([feature canEdit]) {
		propertySubtitle	= [NSString stringWithFormat:@"%@",@"YES"];
	}	
	else {
		propertySubtitle	= [NSString stringWithFormat:@"%@",@"NO"];
	}
	propertyTitle			= [NSString stringWithString:@"canEdit"];
	propertyKind			= iTrackPropetyKingText;
	property				= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
	
	[featureProperties addObject:property];
	
	if ([feature viewLink]) {
		propertyTitle		= [NSString stringWithString:@"viewLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[feature viewLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature identifier]) {
		propertyTitle		= [NSString stringWithString:@"identifier"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[feature identifier]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature publishedDate]) {
		propertyTitle		= [NSString stringWithString:@"publishedDate"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[feature publishedDate] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature updatedDate]) {
		propertyTitle		= [NSString stringWithString:@"updatedDate"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[feature updatedDate] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature editedDate]) {
		propertyTitle		= [NSString stringWithString:@"editedDate"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[feature editedDate] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature ETag]) {
		propertyTitle		= [NSString stringWithString:@"ETag"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[feature ETag]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature fieldSelection]) {
		propertyTitle		= [NSString stringWithString:@"fieldSelection"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[feature fieldSelection]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature kind]) {
		propertyTitle		= [NSString stringWithString:@"kind"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[feature kind]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature resourceID]) {
		propertyTitle		= [NSString stringWithString:@"resourceID"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[feature resourceID]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature title]) {
		propertyTitle		= [NSString stringWithString:@"title"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[feature title] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[property setEditable:YES];
		[property setSetter:@selector(setTitleWithString:)];
		[property setTarget:feature];

		
		[featureProperties addObject:property];
	}
	
	if ([feature summary]) {
		propertyTitle		= [NSString stringWithString:@"summary"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[feature summary] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[property setEditable:YES];
		[property setSetter:@selector(setSummaryWithString:)];
		[property setTarget:feature];
		
		[featureProperties addObject:property];
	}
	
	if ([feature content]) {
		propertyTitle		= [NSString stringWithString:@"content"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[feature content] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature rightsString]) {
		propertyTitle		= [NSString stringWithString:@"rightsString"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[feature rightsString] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	if ([feature links]) {
		propertyTitle		= [NSString stringWithString:@"links [count]"];
		propertySubtitle	= [NSString stringWithFormat:@"%D",[[feature links] count]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature authors]) {
		propertyTitle		= [NSString stringWithString:@"authors [count]"];
		propertySubtitle	= [NSString stringWithFormat:@"%D",[[feature authors] count]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature contributors]) {
		propertyTitle		= [NSString stringWithString:@"contributors [count]"];
		propertySubtitle	= [NSString stringWithFormat:@"%D",[[feature contributors] count]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature categories]) {
		propertyTitle		= [NSString stringWithString:@"categories [count]"];
		propertySubtitle	= [NSString stringWithFormat:@"%D",[[feature categories] count]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature feedLink]) {
		propertyTitle		= [NSString stringWithString:@"feedLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[feature feedLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature editLink]) {
		propertyTitle		= [NSString stringWithString:@"editLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[feature editLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature editMediaLink]) {
		propertyTitle		= [NSString stringWithString:@"editMediaLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[feature editMediaLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature alternateLink]) {
		propertyTitle		= [NSString stringWithString:@"alternateLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[feature alternateLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature relatedLink]) {
		propertyTitle		= [NSString stringWithString:@"relatedLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[feature relatedLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature postLink]) {
		propertyTitle		= [NSString stringWithString:@"postLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[feature postLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature selfLink]) {
		propertyTitle		= [NSString stringWithString:@"selfLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[feature selfLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature HTMLLink]) {
		propertyTitle		= [NSString stringWithString:@"HTMLLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[feature HTMLLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
	
	if ([feature uploadEditLink]) {
		propertyTitle		= [NSString stringWithString:@"uploadEditLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[feature uploadEditLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[featureProperties addObject:property];
	}
}

- (void) loadMapProperties {
	
	// set map properties

	if (mapProperties) {
		for (int i = 0; i < [mapProperties count]; i++) {
			[[mapProperties objectAtIndex:i] release];
		}
		[mapProperties release];
	}
	
	mapProperties = [[NSMutableArray alloc] initWithCapacity:40];

	GDataEntryMap*			map					= [gmaps getMapAtIndex:currentMapId];

	NSString*				propertyTitle		= nil;
	NSString*				propertySubtitle	= nil;
	NSInteger				propertyKind		= iTrackPropetyKingInvalid;

	iTrackPropety*			property			= nil;
	
	if ([map customProperties]) {
		propertyTitle		= [NSString stringWithString:@"customProperties [count]"];
		propertySubtitle	= [NSString stringWithFormat:@"%D",[[map customProperties] count]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
		
		GDataCustomProperty* customProperty		= [[map customProperties] objectAtIndex:0];
		if (customProperty) {
			if ([customProperty name]) {
				propertyTitle		= [NSString stringWithString:@"customProperty [name]"];
				propertySubtitle	= [NSString stringWithFormat:@"%@",[customProperty name]];
				propertyKind		= iTrackPropetyKingText;
				property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
			
				[mapProperties addObject:property];
			}
			if ([customProperty type]) {
				propertyTitle		= [NSString stringWithString:@"customProperty [type]"];
				propertySubtitle	= [NSString stringWithFormat:@"%@",[customProperty type]];
				propertyKind		= iTrackPropetyKingText;
				property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
			
				[mapProperties addObject:property];
			}
			if ([customProperty unit]) {
				propertyTitle		= [NSString stringWithString:@"customProperty [unit]"];
				propertySubtitle	= [NSString stringWithFormat:@"%@",[customProperty unit]];
				propertyKind		= iTrackPropetyKingText;
				property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
			
				[mapProperties addObject:property];
			}
			if ([customProperty value]) {
				propertyTitle		= [NSString stringWithString:@"customProperty [value]"];
				propertySubtitle	= [NSString stringWithFormat:@"%@",[customProperty value]];
				propertyKind		= iTrackPropetyKingText;
				property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
			
				[mapProperties addObject:property];
			}
		}
	}
	
	if ([map isAPIVisible]) {
		propertySubtitle	= [NSString stringWithFormat:@"%@",@"YES"];
	}	
	else {
		propertySubtitle	= [NSString stringWithFormat:@"%@",@"NO"];
	}
	propertyTitle			= [NSString stringWithString:@"isAPIVisible"];
	propertyKind			= iTrackPropetyKingText;
	property				= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
	
	[mapProperties addObject:property];
	
	if ([map canEdit]) {
		propertySubtitle	= [NSString stringWithFormat:@"%@",@"YES"];
	}	
	else {
		propertySubtitle	= [NSString stringWithFormat:@"%@",@"NO"];
	}
	propertyTitle			= [NSString stringWithString:@"canEdit"];
	propertyKind			= iTrackPropetyKingText;
	property				= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
	
	[mapProperties addObject:property];
		
	if ([map viewLink]) {
		propertyTitle		= [NSString stringWithString:@"viewLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[map viewLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map identifier]) {
		propertyTitle		= [NSString stringWithString:@"identifier"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[map identifier]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}

	if ([map publishedDate]) {
		propertyTitle		= [NSString stringWithString:@"publishedDate"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[map publishedDate] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map updatedDate]) {
		propertyTitle		= [NSString stringWithString:@"updatedDate"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[map updatedDate] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map editedDate]) {
		propertyTitle		= [NSString stringWithString:@"editedDate"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[map editedDate] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map ETag]) {
		propertyTitle		= [NSString stringWithString:@"ETag"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[map ETag]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map fieldSelection]) {
		propertyTitle		= [NSString stringWithString:@"fieldSelection"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[map fieldSelection]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map kind]) {
		propertyTitle		= [NSString stringWithString:@"kind"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[map kind]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}

	if ([map resourceID]) {
		propertyTitle		= [NSString stringWithString:@"resourceID"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[map resourceID]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map title]) {
		propertyTitle		= [NSString stringWithString:@"title"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[map title] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[property setEditable:YES];
		[property setSetter:@selector(setTitleWithString:)];
		[property setTarget:map];
		
		[mapProperties addObject:property];
	}
	
	if ([map summary]) {
		propertyTitle		= [NSString stringWithString:@"summary"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[map summary] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[property setEditable:YES];
		[property setSetter:@selector(setSummaryWithString:)];
		[property setTarget:map];
		
		[mapProperties addObject:property];
	}
	
	if ([map content]) {
		propertyTitle		= [NSString stringWithString:@"content"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[map content] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map rightsString]) {
		propertyTitle		= [NSString stringWithString:@"rightsString"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[map rightsString] stringValue]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	if ([map links]) {
		propertyTitle		= [NSString stringWithString:@"links [count]"];
		propertySubtitle	= [NSString stringWithFormat:@"%D",[[map links] count]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map authors]) {
		propertyTitle		= [NSString stringWithString:@"authors [count]"];
		propertySubtitle	= [NSString stringWithFormat:@"%D",[[map authors] count]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map contributors]) {
		propertyTitle		= [NSString stringWithString:@"contributors [count]"];
		propertySubtitle	= [NSString stringWithFormat:@"%D",[[map contributors] count]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map categories]) {
		propertyTitle		= [NSString stringWithString:@"categories [count]"];
		propertySubtitle	= [NSString stringWithFormat:@"%D",[[map categories] count]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map feedLink]) {
		propertyTitle		= [NSString stringWithString:@"feedLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[map feedLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map editLink]) {
		propertyTitle		= [NSString stringWithString:@"editLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[map editLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map editMediaLink]) {
		propertyTitle		= [NSString stringWithString:@"editMediaLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[map editMediaLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map alternateLink]) {
		propertyTitle		= [NSString stringWithString:@"alternateLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[map alternateLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map relatedLink]) {
		propertyTitle		= [NSString stringWithString:@"relatedLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[map relatedLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map postLink]) {
		propertyTitle		= [NSString stringWithString:@"postLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[map postLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map selfLink]) {
		propertyTitle		= [NSString stringWithString:@"selfLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[map selfLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map HTMLLink]) {
		propertyTitle		= [NSString stringWithString:@"HTMLLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[map HTMLLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
	
	if ([map uploadEditLink]) {
		propertyTitle		= [NSString stringWithString:@"uploadEditLink"];
		propertySubtitle	= [NSString stringWithFormat:@"%@",[[[map uploadEditLink] URL] absoluteString]];
		propertyKind		= iTrackPropetyKingText;
		property			= [[iTrackPropety alloc] initWith: propertyTitle subtitle:propertySubtitle kind:propertyKind];
		
		[mapProperties addObject:property];
	}
}

- (iTrackPropety*) getFeaturePropertyByName:(NSString*)name {
	if (featureProperties == nil || name == nil) {
		return nil;
	}
	NSInteger properties_count				= [featureProperties count];
	NSComparisonResult	result				= -1;
	
	for (int i = 0; i < properties_count; i++) {
		iTrackPropety* property				= [featureProperties objectAtIndex:i];
		result								= [[property title] compare:name];
		if (result == 0) {
			return property;
		}
	}
	return nil;
}

- (void) waitForData {
	UIViewController*	indicatorViewController = [[UIViewController alloc] initWithNibName:@"IndicatorView" bundle:nil];
	
	[self presentModalViewController:indicatorViewController animated:YES];
	
	[indicatorViewController release];
}


- (void) stopWaitForData {
	
	if (!mapProperties) {
		[self loadMapProperties];
	}
		
	[[self tableView] reloadData];
	
	UIViewController*	indicatorViewController = [self modalViewController];
	if (indicatorViewController) {
		[indicatorViewController dismissModalViewControllerAnimated:YES];
	}
}

- (void) loadData {
	if (!mapProperties) {
		[self loadMapProperties];
	}
}


#pragma mark -
#pragma mark Initialization


- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if ((self = [super initWithStyle:style])) {
		mapProperties			= nil;
		featureProperties		= nil;
		
		self.child_indexPath	= nil;
		currentFeature			= nil;
		saveCurrentMap			= NO;
		saveCurrentFeature		= NO;
		
    }
    return self;
}

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

	if (gmaps) { 
		[gmaps setActiveView:self]; 
		[gmaps fetchFeaturesOfMap:currentMapId];
	}
			
	if (self.saveCurrentFeature) {
		
		NSInteger     properties_count = [featureProperties count];
		
		for (int i = 0; i < properties_count; i++) {
			iTrackPropety* property = [featureProperties objectAtIndex:i];
			if ([property editable]) {
				id			target	= [property target];
				SEL			setter	= [property setter];
				NSString*	value	= [property subtitle];
				
				[target performSelector:setter withObject:value];
			}
		}
		if (currentFeature) {
			[gmaps setActiveView:self];
			[gmaps updateFeature:currentFeature];
			[self setSaveCurrentFeature:NO]; 
		}
		[[self tableView] reloadData];
	}
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

	if (self.saveCurrentMap) {
		
		NSInteger	properties_count = [mapProperties count];
		
		for (int i = 0; i < properties_count; i++) {
			iTrackPropety* property = [mapProperties objectAtIndex:i];
			if ([property editable]) {
				id			target	= [property target];
				SEL			setter	= [property setter];
				NSString*	value	= [property subtitle];
				
				[target performSelector:setter withObject:value];
			}
		}
		[gmaps setActiveView:root_controller];
		[gmaps updateMap:currentMapId];
		[self setSaveCurrentMap:NO];
		
		if (root_controller) {
			[root_controller setUpdated:YES];
		}
		
		// [[self tableView] reloadData];
	
	}
}

/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	
	NSString* SectionTitle;
	if (section == 0) {
		SectionTitle = [[[NSString alloc] initWithString:@"FEATURES"] autorelease]; 
	}
	else {
		SectionTitle = [[[NSString alloc] initWithString:@"PROPERTIES"] autorelease];
	}
	return SectionTitle;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (section == 0) {
		if (gmaps) {
			return [gmaps countOfFeaturesOfMap:currentMapId];
		}
	}
	else {
		if (mapProperties) {
			return [mapProperties count];
		}
	}
	return 0;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
	
	if (indexPath.section == 1) {
		if (mapProperties == nil) {
			return cell;
		}
		iTrackPropety* property = [mapProperties objectAtIndex:indexPath.row];
		if (property) {
			[[cell textLabel] setText:[property title]];
			[[cell detailTextLabel] setText:[property subtitle]];
		}		
		return cell;	
	}
	
	if (gmaps == nil) {
		return cell;
	}
	
    GDataEntryMapFeature* feature	= [gmaps getFeatureOfMap:currentMapId AtIndex:indexPath.row];
	
	if (feature == nil) {
		return cell;
	}
	
    // Configure the cell...
	
	GDataTextConstruct*	feature_title		= [feature title];
	NSArray*			feature_KMLValues	= [feature KMLValues];
	GDataXMLNode*		featureXMLNode		= [feature_KMLValues objectAtIndex:0];
	NSMutableString*	featureSummary		= [[NSMutableString alloc] initWithCapacity: 1024];
	
	if (featureXMLNode) {
		NSUInteger		node_childCount		= [featureXMLNode childCount];
		GDataXMLNode*	node_child;
		
		[featureSummary appendFormat:@"%@:",[featureXMLNode name]];
		
		for (int i = 0; i < node_childCount; i++) {
			if (i > 0) {
				[featureSummary appendFormat:@"%@",@","];
			}
			node_child = [featureXMLNode childAtIndex:i];
			[featureSummary appendFormat:@" %@",[node_child name]];
		}
	}
	
    [[cell textLabel] setText:[feature_title stringValue]];
	[[cell detailTextLabel] setText:featureSummary];
	
	[featureSummary release];
	
    return cell;
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    if (indexPath.section > 0) {
		return NO;
	}
	return YES;
}




// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
	if (indexPath.section > 0) {
		return;
	}
	
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
		[gmaps setActiveView:self];
		[gmaps deleteFeatureOfMap:currentMapId AtIndex:indexPath.row];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}



/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	
	if (indexPath.section == 1) {
	
		if ([[mapProperties objectAtIndex:indexPath.row] kind] == iTrackPropetyKingText) {
			
			UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"XMLTextView" bundle:nil];
			UITextView* textView = (UITextView*) [detailViewController view];
		
			[textView setText:[(iTrackPropety*) [mapProperties objectAtIndex:indexPath.row] subtitle]];
			[textView setEditable:[(iTrackPropety*) [mapProperties objectAtIndex:indexPath.row] editable]];
			
			self.child_indexPath = indexPath;
			
			[textView setDelegate:self];
			
			[self.navigationController pushViewController:detailViewController animated:YES];
			[detailViewController release];
			
			return;
		}
	}
	
    GDataEntryMapFeature*	feature				= [gmaps getFeatureOfMap:currentMapId AtIndex:indexPath.row];
	
	if (feature == nil) {
		return;
	}
	
	currentFeature								= feature;
	
	XMLNodeViewController *detailViewController = [[XMLNodeViewController alloc] initWithNibName:@"XMLNodeView" bundle:nil];
	NSArray*				feature_KMLValues	= [feature KMLValues];
	GDataXMLNode*			featureXMLNode		= [feature_KMLValues objectAtIndex:0];
	
	[self loadFeatureProperties:feature];
	
	[detailViewController setRoot_node:featureXMLNode];
	[detailViewController setFeatureProperties:[self featureProperties]];
	[detailViewController setEditable:NO];
	[detailViewController setUpdated:NO];
	[detailViewController setRoot_controller:self];
	
	// Pass the selected object to the new view controller.
	[self.navigationController pushViewController:detailViewController animated:YES];
	[detailViewController release];
	
}

#pragma mark -
#pragma mark update the map

- (IBAction)addClicked:(id)sender {
}

#pragma mark -
#pragma mark text editing protocol

- (void)textViewDidEndEditing:(UITextView *)textView {
	
	if (self.child_indexPath.section == 1 && mapProperties) {
		iTrackPropety*	property	= [mapProperties objectAtIndex:self.child_indexPath.row];
		
		[property setSubtitle:[textView text]];
		
		[self setSaveCurrentMap:YES];
		
		[[self tableView] reloadData];
	}	
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	if (mapProperties) {
		for (int i = 0; i < [mapProperties count]; i++) {
			[[mapProperties objectAtIndex:i] release];
		}
		[mapProperties release];
	}
	
	if (featureProperties) {
		for (int i = 0; i < [featureProperties count]; i++) {
			[[featureProperties objectAtIndex:i] release];
		}
		[featureProperties release];
	}
    
	[super dealloc];
}


@end

